package com.zelin.web.servlet;

import com.alibaba.fastjson.JSON;
import com.zelin.pojo.Classes;
import com.zelin.pojo.R;
import com.zelin.service.ClassesService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/11/10-9:33
 * ------------------------------
 */
@WebServlet("/classes")
public class ClassesServlet extends HttpServlet {

    private ClassesService classesService;

    @Override
    public void init() throws ServletException {
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        classesService = wac.getBean(ClassesService.class);
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 查询所有班级
        List<Classes> classeses = classesService.findAll();
        //2. 输出
        resp.getWriter().println(JSON.toJSONString(R.ok().data("list",classeses)));
        resp.getWriter().flush();
    }
}
